package nju.bishe.weibo.adapter;

import java.util.List;

import nju.bishe.weibo.R;
import nju.bishe.weibo.data.UserToken;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class UserTokenAdapter extends BaseAdapter {
	
	 private static final String CLASSTAG = UserTokenAdapter.class.getSimpleName();
     private final Context context;
	 private final List<UserToken> reviews;
	 private  ViewHolder vh=new ViewHolder();

	    public UserTokenAdapter(Context context, List<UserToken> reviews) {
	        this.context = context;
	        this.reviews = reviews;
	        Log.v("weiboNJUers", " " + CLASSTAG + " reviews size - " + this.reviews.size());
	    }
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		 return this.reviews.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return this.reviews.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		UserToken tokenWrapper = this.reviews.get(position);
		View login_item = LayoutInflater.from(context).inflate(R.layout.login_item,null);
		vh.login_image=(ImageView)login_item.findViewById(R.id.login_image);
		vh.login_id=(TextView)login_item.findViewById(R.id.login_id);
		vh.login_name=(TextView)login_item.findViewById(R.id.login_name);
		
		vh.login_image.setImageBitmap(BitmapFactory.decodeByteArray(tokenWrapper.getIcon(), 0, tokenWrapper.getIcon().length));
		vh.login_id.setText(tokenWrapper.getUser_id());
		vh.login_name.setText(tokenWrapper.getUserName());
		return login_item;
		//return new UserTokenListView(this.context,tokenWrapper.getUserName(),tokenWrapper.getIcon());
	}
	
    private final class UserTokenListView extends LinearLayout {

        private TextView username;
        private ImageView userImage;
        public UserTokenListView(Context context, String username, byte[] icon) {

            super(context);
            setOrientation(LinearLayout.HORIZONTAL);

            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
            params.setMargins(5, 3, 5, 0);

            this.userImage = new ImageView(context);
            userImage.setImageBitmap(BitmapFactory.decodeByteArray(icon, 0, icon.length));
            this.addView(this.userImage,params);
            
            this.username = new TextView(context);
            this.username.setText(username);
            this.username.setTextSize(16f);
            this.username.setTextColor(Color.BLUE);
            this.addView(this.username, params);
            
        }
    }
    


	private static class ViewHolder{
		ImageView login_image;
		TextView login_name;//昵称
		TextView login_id;//时间
		}

}
